package com.gopay.bis.manager.bank.tpas;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.domain.bank.tpas.TpasMessage;
import com.gopay.common.domain.bank.tpas.TpasResult;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.MD5Util;
import com.gopay.remote.bank.TpasChargeInteractRemoteService;

public class TpasOfpayManager {
	private final static Logger logger = LoggerFactory.getLogger(TpasOfpayManager.class);

    @Resource(name = "ofpayServerClient")
    private TpasChargeInteractRemoteService ofpayServerClient;
    @Resource(name = "isTest")
    private Map<String, String> isTest;
    
	private static final Map<String,Map<String, String>> REL = new HashMap<String, Map<String,String>>();
	static {
		//中石化
		Map<String, String> sinopec = new HashMap<String, String>();
			sinopec.put("50", "64157005");
			sinopec.put("100", "64157004");
			sinopec.put("500", "64157002");
			sinopec.put("1000", "64157001");
			sinopec.put("ARBITRARILY", "64127500");		//中石化任意充
		//中石油
		Map<String, String> cnpc = new HashMap<String, String>();
		
		REL.put("1", sinopec);
		REL.put("2", cnpc);
		
	}

    public TpasResult mobileBelong(TpasMessage base) throws GopayException {
        return ofpayServerClient.mobileBelong(base);
    }

    public TpasResult mobileCharge(TpasMessage base) throws GopayException {
        MobileMessage msg = (MobileMessage)base;
        String packBoby = msg.getMerID() + msg.getPrivatePwd() + msg.getCardId() + msg.getCardNum() + msg.getBankOrderId() + msg.getOrderDate() + msg.getOuterCustID();
        String plainMessage = MD5Util.sign(packBoby + base.getPrivatePwd2nd());
        base.setPlainMessage(plainMessage);
        return ofpayServerClient.mobileCharge(base);
    }

    public TpasResult flowCharge(TpasMessage base) throws GopayException {
//        包体= userid + userpws + phoneno + perValue + flowValue + range + effectStartTime + effectTime
//                netType+ sporderId
        FlowMessage msg = (FlowMessage)base;
        msg.setRange("00".equals(msg.getRange()) ? "2" : "1");
        String packBoby = msg.getMerID() + msg.getPrivatePwd() + msg.getOuterCustID() + msg.getPerValue() + msg.getFlowValue() 
                + msg.getRange() + msg.getEffectStartTime() + msg.getEffectTime() + (StringUtils.isNotBlank(msg.getNetType())?msg.getNetType():"") + msg.getBankOrderId();
        String plainMessage = MD5Util.sign(packBoby + msg.getPrivatePwd2nd());
        msg.setPlainMessage(plainMessage);
        return ofpayServerClient.flowCharge(msg);
    }
    
    public TpasResult oilCardInfo(TpasMessage base) throws GopayException {
//        包体=userid+userpws+game_userid
        OilCardMessage msg = (OilCardMessage)base;
        String packBoby = msg.getMerID() + msg.getPrivatePwd() + msg.getOuterCustID();
        String plainMessage = MD5Util.sign(packBoby + base.getPrivatePwd2nd());
        base.setPlainMessage(plainMessage);
        return ofpayServerClient.oilCardInfo(base);
    }

    public TpasResult oilCardCharge(TpasMessage base) throws GopayException {
//        userid+userpws+cardid+cardnum+sporder_id+sporder_time+ game_userid
        OilCardMessage msg = (OilCardMessage)base;
        Map<String, String> rel = REL.get(msg.getCardType());//通过不同的卡取出供应商对应关系
        //测试环境支持任意充
        String cardid = StringUtils.isNotEmpty(rel.get(msg.getCardId())) ? rel.get(msg.getCardId()) 
        		: (StringUtils.equals(isTest.get("isTest"), "1") ? rel.get("ARBITRARILY") : "");
        if(StringUtils.isBlank(cardid)){
        	logger.info("ofpay oilCard charge error:不支持的面值" + ToStringBuilder.reflectionToString(base, ToStringStyle.DEFAULT_STYLE));
        	throw new GopayException("不支持的面值");
        }
        msg.setCardId(cardid);
        String packBoby = msg.getMerID() + msg.getPrivatePwd() + msg.getCardId() + msg.getCardNum() + msg.getBankOrderId()
                + msg.getOrderDate() + msg.getOuterCustID();
        String plainMessage = MD5Util.sign(packBoby + base.getPrivatePwd2nd());
        base.setPlainMessage(plainMessage);
        return ofpayServerClient.oilCardCharge(base);
    }

    public TpasResult queryOrderStatus(TpasMessage base) throws GopayException {
        return ofpayServerClient.queryOrderStatus(base);
    }

}
